// ActionScript file
// 图片处理的左则工具栏
import cordy.tools.filters;
import com.gskinner.geom.ColorMatrix;
import mx.controls.*;
import mx.events.SliderEvent;


[Bindable] private var brightness_value:Number = new Number(); // 高度调节数值
[Bindable] private var contrast_value:Number = new Number();
[Bindable] private var saturation_value:Number = new Number();
[Bindable] private var hue_value:Number = new Number();

private var cm:ColorMatrix = new ColorMatrix();

/**
 * 向左边栏添加调节工具
 */
private function effectControls():DisplayObject
{
	var vbox_ctr:VBox=new VBox();
	vbox_ctr.setStyle("horizontalCenter", 0);
	vbox_ctr.x=0;
	vbox_ctr.y=0;
	vbox_ctr.percentWidth=100;
	//vbox_ctr.width = 220;
	vbox_ctr.percentHeight=100;
	//vbox_ctr.setStyle("backgroundColor", '0xcccccc');
	//vbox_ctr.setStyle("backgroundAlpha", 100);
	
	var label_ctr_brightness:Label=new Label();
	label_ctr_brightness.text='亮度';
	label_ctr_brightness.percentWidth=100;
	//label_ctr_brightness.x = (vbox_ctr.width - label_ctr_brightness.width)/2;
	label_ctr_brightness.x=0;
	label_ctr_brightness.setStyle("textAlign", "center");
	var hslider_ctr_brightness:HSlider=new HSlider();
	hslider_ctr_brightness.minimum=-100;
	hslider_ctr_brightness.maximum=100;
	hslider_ctr_brightness.value=0;
	hslider_ctr_brightness.percentWidth=100;
	hslider_ctr_brightness.id = 'hs_bri';
	hslider_ctr_brightness.name = 'hs_bri';
	hslider_ctr_brightness.snapInterval = 1;
	hslider_ctr_brightness.addEventListener(SliderEvent.CHANGE, function(event:SliderEvent):void{brightness_value = event.target.value;changeBrightness();});
	
	var label_ctr_contrast:Label=new Label();
	label_ctr_contrast.text='对比度';
	label_ctr_contrast.percentWidth=100;
	label_ctr_contrast.setStyle("textAlign", "center");
	var hslider_ctr_contrast:HSlider=new HSlider();
	hslider_ctr_contrast.minimum=-100;
	hslider_ctr_contrast.maximum=100;
	hslider_ctr_contrast.value=0;
	hslider_ctr_contrast.percentWidth=100;
	hslider_ctr_contrast.id = 'hs_con';
	hslider_ctr_contrast.addEventListener(SliderEvent.CHANGE, function(event:SliderEvent):void{contrast_value = event.target.value;changeContrast();});
	
	var label_ctr_saturation:Label=new Label();
	label_ctr_saturation.text='饱和度';
	label_ctr_saturation.percentWidth=100;
	label_ctr_saturation.setStyle("textAlign", "center");
	var hslider_ctr_saturation:HSlider=new HSlider();
	hslider_ctr_saturation.minimum=-100;
	hslider_ctr_saturation.maximum=100;
	hslider_ctr_saturation.value=0;
	hslider_ctr_saturation.percentWidth=100;
	hslider_ctr_saturation.id = 'hs_sat';
	hslider_ctr_saturation.addEventListener(SliderEvent.CHANGE,function(event:SliderEvent):void{saturation_value = event.target.value;changeSaturation()});
	
	var label_ctr_hue:Label=new Label();
	label_ctr_hue.text='色调';
	label_ctr_hue.percentWidth=100;
	label_ctr_hue.setStyle("textAlign", "center");
	var hslider_ctr_hue:HSlider=new HSlider();
	hslider_ctr_hue.minimum=-180;
	hslider_ctr_hue.maximum=180;
	hslider_ctr_hue.value=0;
	hslider_ctr_hue.percentWidth=100;
	hslider_ctr_hue.id = 'hs_hue';
	hslider_ctr_hue.addEventListener(SliderEvent.CHANGE, function(event:SliderEvent):void{hue_value = event.target.value;changeHue();});
	
	/*
	var effectOK:Button = new Button();
	effectOK.label = '';
	effectOK.percentWidth = 100;
	effectOK.addEventListener(MouseEvent.CLICK, saveEffect);
	*/
	
	vbox_ctr.addChild(label_ctr_brightness);
	vbox_ctr.addChild(hslider_ctr_brightness);
	vbox_ctr.addChild(label_ctr_contrast);
	vbox_ctr.addChild(hslider_ctr_contrast);
	vbox_ctr.addChild(label_ctr_saturation);
	vbox_ctr.addChild(hslider_ctr_saturation);
	vbox_ctr.addChild(label_ctr_hue);
	vbox_ctr.addChild(hslider_ctr_hue);
	//vbox_ctr.addChild(effectOK);
	
	return vbox_ctr;
}
/**
 * 我的照片点击事件。
 */
private function onEffect(value:Object):void{
	trace('in effect()');
	//var img1:Image = new Image();
	effect_img.setStyle('horizontalCenter', '0');
	effect_img.setStyle('verticalCenter', '0');
	effect_img.source = value.urlx;
	effect_img.width = value.width;
	effect_img.height = value.height;
	effect_img.id = 'effect_img';
	effect_img.name = 'effect_img';
	mainpaint.addChild(effect_img);
	mainpaint.setChildIndex(effect_img, 0);
	trace(value);
	this.currentPhoto = effect_img.source.toString();
}
/**
 * 特效处理事件
 */
private function doEffect(value:Object):void{
	trace(value);
	trace(effect_img.source);
	//var stmp:String = effect_img.source.toString();
	//trace(stmp);
	if(this.currentPhoto == ''){
		Alert.show('请先选择一张照片','注意');
		return;
	}
	
	switch(value){
		case 'bw': //黑白
			trace(effect_img.width);
			trace(effect_img.height);
			var inputbw:BitmapData=new BitmapData(effect_img.width, effect_img.height);
			inputbw.draw(effect_img);
			var outbw:Bitmap=new Bitmap(cordy.tools.filters.grayFilter(inputbw));
			effect_img.source = outbw;
			break;
		case 'invert': //反色
			var inputinvert:BitmapData=new BitmapData(effect_img.width, effect_img.height);
			inputinvert.draw(effect_img);
			var outinvert:Bitmap=new Bitmap(cordy.tools.filters.invert(inputinvert));
			effect_img.source = outinvert;
			trace('invert');
			break;
		case 'emboss': //浮雕
			var inputemboss:BitmapData=new BitmapData(effect_img.width, effect_img.height);
			inputemboss.draw(effect_img);
			var outemboss:Bitmap=new Bitmap(cordy.tools.filters.embossFilter(inputemboss));
			effect_img.source = outemboss;
			trace('emboss');
			break;
		case 'old': //老照片
			var inputold:BitmapData=new BitmapData(effect_img.width, effect_img.height);
			inputold.draw(effect_img);
			var outold:Bitmap=new Bitmap(cordy.tools.filters.oldPictureFilter(inputold));
			effect_img.source = outold;
			trace('old');
			break;
		case 'blur': //模糊
			var inputblur:BitmapData=new BitmapData(effect_img.width, effect_img.height);
			inputblur.draw(effect_img);
			var outblur:Bitmap=new Bitmap(cordy.tools.filters.blurFilter(inputblur));
			effect_img.source = outblur;
			trace('blur');
			break;
		case 'spherize': //中心球形模糊
			var inputspherize:BitmapData=new BitmapData(effect_img.width, effect_img.height);
			inputspherize.draw(effect_img);
			var outspherize:Bitmap=new Bitmap(cordy.tools.filters.spherizeFilter(inputspherize));
			effect_img.source = outspherize;
			trace('spherize');
			break;
		default :
			trace('doEffect is default.');
			break;
	}
}
/**
 * 调节亮度
 */
private function changeBrightness():void{
	trace(brightness_value);
	if(this.currentPhoto != ''){
		trace('do brightness');
		cm.adjustColor(brightness_value, contrast_value, saturation_value, hue_value);
		//cm.adjustColor(brightness_value,0, 0, 0);
		effect_img.filters = [new ColorMatrixFilter(cm)];
	}
}
/**
 * 调节对比度
 */
private function changeContrast():void{
	if(this.currentPhoto != ''){
		trace('do contrast');
		cm.adjustColor(brightness_value, contrast_value, saturation_value, hue_value);
		//cm.adjustColor(brightness_value,0, 0, 0);
		effect_img.filters = [new ColorMatrixFilter(cm)];
	}
}
/**
 * 调节饱和度
 */
private function changeSaturation():void{
	if(this.currentPhoto != ''){
		trace('do saturation');
		cm.adjustColor(brightness_value, contrast_value, saturation_value, hue_value);
		//cm.adjustColor(brightness_value,0, 0, 0);
		effect_img.filters = [new ColorMatrixFilter(cm)];
	}
}
/**
 * 调节色调
 */
private function changeHue():void{
	if(this.currentPhoto != ''){
		trace('do hue');
		cm.adjustColor(brightness_value, contrast_value, saturation_value, hue_value);
		//cm.adjustColor(brightness_value,0, 0, 0);
		effect_img.filters = [new ColorMatrixFilter(cm)];
	}
}